#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define EI_NIDENT 16

typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Word;
typedef uint32_t Elf32_Addr;
typedef uint32_t Elf32_Off;

typedef struct {
        unsigned char   e_ident[EI_NIDENT];
        Elf32_Half      e_type;
        Elf32_Half      e_machine;
        Elf32_Word      e_version;
        Elf32_Addr      e_entry;
        Elf32_Off       e_phoff;
        Elf32_Off       e_shoff;
        Elf32_Word      e_flags;
        Elf32_Half      e_ehsize;
        Elf32_Half      e_phentsize;
        Elf32_Half      e_phnum;
        Elf32_Half      e_shentsize;
        Elf32_Half      e_shnum;
        Elf32_Half      e_shstrndx;
} Elf32_Ehdr;

#define ELFCLASS32 1
#define ELFCLASS64 2

#define ELFDATA2LSB 1
#define ELFDATA2MSB 2

#define EV_CURRENT 1

#define ELFOSABI_LINUX 3

#define ET_REL 1
#define ET_EXEC 2

#define EM_NONE 0
#define EM_X86_64 62

#define EV_CURRENT 1

void print_elf(Elf32_Ehdr hdr);

void main(int argc, char **argv) {
	Elf32_Ehdr hdr;
	
	hdr.e_ident[0] = 0x7f;
	hdr.e_ident[1] = 'E';
	hdr.e_ident[2] = 'L';
	hdr.e_ident[3] = 'F';

	// class
	hdr.e_ident[4] = ELFCLASS32;

	// data
	hdr.e_ident[5] = ELFDATA2LSB;

	// version
	hdr.e_ident[6] = EV_CURRENT;

	// osabi
	hdr.e_ident[7] = ELFOSABI_LINUX;

	// abiversion
	hdr.e_ident[8] = 0;

	// pad
	hdr.e_ident[9] = 0;
	
	hdr.e_type = ET_EXEC;
	hdr.e_machine = EM_NONE;
	hdr.e_version = EV_CURRENT;

	print_elf(hdr);
}

void octet(unsigned char c) {
	printf("%02x", c);
}

void oct2(uint16_t c) {
	printf("%02x %02x", c&0xFF, (c>>8)&0xFF);
}

void print_elf(Elf32_Ehdr hdr) {
	int i;
	
	for(i = 0; i < EI_NIDENT; i++) {
		octet(hdr.e_ident[i]);
		if(i != EI_NIDENT-1)
			printf(" ");
	}
	puts(" # e_ident");

	oct2(hdr.e_type);
	puts(" # e_type");

	oct2(hdr.e_machine);
	puts(" # e_machine");
	
	oct2(hdr.e_version);
	puts(" # e_version");
	
        /* Elf32_Addr      e_entry; */
        /* Elf32_Off       e_phoff; */
        /* Elf32_Off       e_shoff; */
        /* Elf32_Word      e_flags; */
        /* Elf32_Half      e_ehsize; */
        /* Elf32_Half      e_phentsize; */
        /* Elf32_Half      e_phnum; */
        /* Elf32_Half      e_shentsize; */
        /* Elf32_Half      e_shnum; */
        /* Elf32_Half      e_shstrndx; */
}
